package com.qidianit.mes.pro.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.WorkflowProduct;
import com.qidianit.mes.pro.mapper.WorkflowProductMapper;
import com.qidianit.mes.pro.service.WorkflowProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class WorkflowProductServiceImpl implements WorkflowProductService {

    @Autowired
    private WorkflowProductMapper workflowProductMapper;

    /* ==================== 接口13 ==================== */
    @Override
    public Result<PageInfo<WorkflowProduct>> selectPage(Long workflowId,
                                                        Integer page,
                                                        Integer size) {
        if (workflowId == null) {
            return Result.error("workflowId不能为空");
        }
        PageHelper.startPage(page, size);
        return Result.success(PageInfo.of(workflowProductMapper.selectPage(workflowId,
                (page - 1) * size, size)));
    }

    /* ==================== 接口14 ==================== */
    @Override
    @Transactional
    public Result<String> add(WorkflowProduct wp) {
        if (wp.getWorkflowId() == null
                || StringUtils.isBlank(wp.getProductCode())) {
            return Result.error("参数不完整");
        }
        if (workflowProductMapper.countExists(wp.getWorkflowId(),
                wp.getProductCode()) > 0) {
            return Result.error("该产品/物料已经添加");
        }
        // 当前登录人 TODO: 按自己框架工具替换
        wp.setCreateBy("system");
        return workflowProductMapper.insert(wp) == 1
                ? Result.success(null)
                : Result.error("操作失败");
    }

    /* ==================== 接口15 ==================== */
    @Override
    public Result<WorkflowProduct> selectById(Long id) {
        if (id == null) {
            return Result.error("ID不能为空");
        }
        WorkflowProduct wp = workflowProductMapper.selectById(id);
        return wp == null
                ? Result.error("记录不存在或已删除")
                : Result.success(wp);
    }

    /* ==================== 接口16 ==================== */
    @Override
    @Transactional
    public Result<WorkflowProduct> updateProduct(WorkflowProduct wp) {
        if (wp.getId() == null
                || wp.getWorkflowId() == null
                || StringUtils.isBlank(wp.getProductCode())) {
            return Result.error("参数不完整");
        }
        // 排除自己后重复校验
        if (workflowProductMapper.countExistsExcludeSelf(wp.getWorkflowId(),
                wp.getProductCode(), wp.getId()) > 0) {
            return Result.error("该物料已存在");
        }
        return workflowProductMapper.updateProduct(wp) == 1
                ? Result.success(wp)
                : Result.error("操作失败");
    }

    /* ==================== 接口17 ==================== */
    @Override
    @Transactional
    public Result<String> deleteProduct(Long id) {
        if (id == null) {
            return Result.error("ID不能为空");
        }
        return workflowProductMapper.deleteProduct(id) == 1
                ? Result.success(null)
                : Result.error("操作失败");
    }

    /* ==================== 接口18 ==================== */
    @Override
    @Transactional
    public Result<String> batchDelete(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Result.error("参数为空");
        }
        return workflowProductMapper.batchProduct(ids) > 0
                ? Result.success(null)
                : Result.error("操作失败");
    }
}